$(function () {
    //    先去请求对应sid的个人信息
    let data = {};
    let state = localStorage.getItem("state");
    let form = $("#storeForm");
    getInfo();

    //第一次获取数据
    function getInfo() {
        if (state === "3") {
            $.ajax({
                type: "Get",
                url: "/store?method=getStoreData",
                data: {sId: localStorage.getItem("sId")},
                success: function (res) {
                    data = JSON.parse(res);
                    $("#name").val(data.name);
                    $("#cardId").val(data.cardId);
                    // $("#cardImg").val(data.cardImg);
                    data.cardImg ? $(".cardImg").attr("src", data.cardImg) : $(".cardImg").css("display", "none");
                    $("#storeAddr").val(data.storeAddr);
                    $("#storeName").val(data.storeAddr);
                    $("#businessId").val(data.businessId);
                    $("#storeId").val(data.id);
                },
            })
        } else {
            let sid = localStorage.getItem("sId");
            $("#storeId").val(sid == 0 ? null : sid);
        }

    }

    function submitData() {
        let formData = new FormData($("#storeForm")[0]);
        $.ajax({
            url: "/store",
            method: "POST",
            data: formData,
            cache: false,
            processData: false,
            contentType: false,
            success: (res) => {
                let data = JSON.parse(res);
                if (data.msg) {
                    $(".msgSuc").show();
                    setTimeout(() => {
                        $(".msgSuc").hide();
                        window.location.href = "../html/login.html";
                    }, 2000)
                } else {
                    // getInfo();
                    $(".msgErr").show();
                    setTimeout(() => {
                        $(".msgErr").hide();
                    }, 2000)
                }
            }
        })
    }

    //根据校验结果是否发请求
    $("#apply_store").on("click", function (e) {
        e.preventDefault();
        form.data("bootstrapValidator").validate();
        let flag = form.data("bootstrapValidator").isValid();
        if (flag) submitData()
    });
//    表单校验
    let formFiled = {
        cardId: {
            validators: {
                notEmpty: {
                    message: '身份证不能为空'
                },
                regexp: {
                    regexp: /(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/,
                    message: "身份证格式有误"
                }
            }
        },
        name: {
            validators: {
                notEmpty: {
                    message: '真实姓名不能为空'
                },
                stringLength: {
                    min: 2,
                    max: 18,
                    message: '真实姓名长度必须在2到18位之间'
                },
                regexp: {
                    regexp: /^[a-zA-Z0-9\u4e00-\u9fa5]+$/,
                    message: '真实姓名只能包含汉字、字母、数字'
                }
            }
        },
        storName: {
            validators: {
                notEmpty: {
                    message: '商店名称不能为空'
                },
                stringLength: {
                    min: 2,
                    max: 18,
                    message: '商店名称长度必须在2到18位之间'
                },
                regexp: {
                    regexp: /^[a-zA-Z0-9\u4e00-\u9fa5]+$/,
                    message: '商店名称只能包含汉字、字母、数字'
                }
            }
        },
        businessId: {
            validators: {
                notEmpty: {
                    message: '营业证件号不能为空'
                },
                regexp: {
                    regexp: /^[0-9]*$/,
                    message: '营业证件号只能是数字'
                }
            }
        },
        cardImg: {
            validators: {
                notEmpty: {
                    message: '身份证照片不能为空'
                }
            },
        },
        storeName: {
            validators: {
                notEmpty: {
                    message: '商店名称不能为空'
                }
            }
        },
        storeAddr: {
            validators: {
                notEmpty: {
                    message: '商店地址不能为空'
                }
            }
        },
    }
    if (state === "3") formFiled.cardImg = {};
    form.bootstrapValidator({fields: formFiled})
})